<!DOCTYPE html>
<html lang="en" data-color-mode="auto" data-light-theme="light" data-dark-theme="dark">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>G4F - Authentication Guide</title>
    <link rel="apple-touch-icon" sizes="180x180" href="/dist/img/apple-touch-icon.png">
    <link rel="icon" type="image/png" sizes="32x32" href="/dist/img/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="/dist/img/favicon-16x16.png">
    <link rel="manifest" href="/dist/img/site.webmanifest">
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/light-74231a1f3bbb.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/dark-8a995f0bacd4.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/primer-primitives-225433424a87.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/primer-b8b91660c29d.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/global-205098e9fedd.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/code-177d21388df8.css" />
    <style>
        :root {
            --colour-1: #000000;
            --colour-2: #ccc;
            --colour-3: #e4d4ff;
            --colour-4: #f0f0f0;
            --colour-5: #181818;
            --colour-6: #242424;
            --accent: #8b3dff;
            --gradient: #1a1a1a;
            --background: #16101b;
            --size: 70vw;
            --top: 50%;
            --blur: 40px;
            --opacity: 0.6;
        }

        /* Body and text color */
        body {
            height: 100vh;
            margin: 0;
            padding: 0;
        }

        .hidden {
            display: none;
        }

        .container-lg {
            margin: 0 auto;
            padding: 8px;
        }

        @media only screen and (min-width: 40em) {
            .container-lg {
                max-width: 84%;
            }
        }
    </style>
</head>
<body>
    <article class="markdown-body entry-content container-lg" itemprop="text"><div class="markdown-heading"><h2 class="heading-element">G4F - Authentication Guide</h2><a id="user-content-g4f---authentication-guide" class="anchor" aria-label="Permalink: G4F - Authentication Guide" href="#g4f---authentication-guide"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p>This documentation explains how to authenticate with G4F providers and configure GUI security. It covers API key management, cookie-based authentication, rate limiting, and GUI access controls.</p>
<hr>
<div class="markdown-heading"><h2 class="heading-element"><strong>Table of Contents</strong></h2><a id="user-content-table-of-contents" class="anchor" aria-label="Permalink: Table of Contents" href="#table-of-contents"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<ol>
<li>
<strong><a href="#provider-authentication">Provider Authentication</a></strong>
<ul>
<li><a href="#prerequisites">Prerequisites</a></li>
<li><a href="#api-key-setup">API Key Setup</a></li>
<li><a href="#synchronous-usage">Synchronous Usage</a></li>
<li><a href="#asynchronous-usage">Asynchronous Usage</a></li>
<li><a href="#multiple-providers-with-api-keys">Multiple Providers</a></li>
<li><a href="#cookie-based-authentication">Cookie-Based Authentication</a></li>
<li><a href="#rate-limiting">Rate Limiting</a></li>
<li><a href="#error-handling">Error Handling</a></li>
<li><a href="#supported-providers">Supported Providers</a></li>
</ul>
</li>
<li>
<strong><a href="#gui-authentication">GUI Authentication</a></strong>
<ul>
<li><a href="#server-setup">Server Setup</a></li>
<li><a href="#browser-access">Browser Access</a></li>
<li><a href="#programmatic-access">Programmatic Access</a></li>
</ul>
</li>
<li><strong><a href="#best-practices">Best Practices</a></strong></li>
<li><strong><a href="#troubleshooting">Troubleshooting</a></strong></li>
</ol>
<hr>
<div class="markdown-heading"><h2 class="heading-element"><strong>Provider Authentication</strong></h2><a id="user-content-provider-authentication" class="anchor" aria-label="Permalink: Provider Authentication" href="#provider-authentication"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<div class="markdown-heading"><h3 class="heading-element"><strong>Prerequisites</strong></h3><a id="user-content-prerequisites" class="anchor" aria-label="Permalink: Prerequisites" href="#prerequisites"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<ul>
<li>Python 3.7+</li>
<li>Installed <code>g4f</code> package:
<div class="highlight highlight-source-shell"><pre>pip install g4f</pre></div>
</li>
<li>API keys or cookies from providers (if required).</li>
</ul>
<hr>
<div class="markdown-heading"><h3 class="heading-element"><strong>API Key Setup</strong></h3><a id="user-content-api-key-setup" class="anchor" aria-label="Permalink: API Key Setup" href="#api-key-setup"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<div class="markdown-heading"><h4 class="heading-element"><strong>Step 1: Set Environment Variables</strong></h4><a id="user-content-step-1-set-environment-variables" class="anchor" aria-label="Permalink: Step 1: Set Environment Variables" href="#step-1-set-environment-variables"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p><strong>For Linux/macOS (Terminal)</strong>:</p>
<div class="highlight highlight-source-shell"><pre><span class="pl-c"><span class="pl-c">#</span> Example for Anthropic</span>
<span class="pl-k">export</span> ANTHROPIC_API_KEY=<span class="pl-s"><span class="pl-pds">"</span>your_key_here<span class="pl-pds">"</span></span>

<span class="pl-c"><span class="pl-c">#</span> Example for HuggingFace</span>
<span class="pl-k">export</span> HUGGINGFACE_API_KEY=<span class="pl-s"><span class="pl-pds">"</span>another_key_here<span class="pl-pds">"</span></span></pre></div>
<p><strong>For Windows (Command Prompt)</strong>:</p>
<div class="highlight highlight-source-batchfile"><pre><span class="pl-c"><span class="pl-c">::</span> Example for Anthropic</span>
<span class="pl-k">set</span> <span class="pl-smi">ANTHROPIC_API_KEY</span><span class="pl-k">=</span>your_key_here

<span class="pl-c"><span class="pl-c">::</span> Example for HuggingFace</span>
<span class="pl-k">set</span> <span class="pl-smi">HUGGINGFACE_API_KEY</span><span class="pl-k">=</span>another_key_here</pre></div>
<p><strong>For Windows (PowerShell)</strong>:</p>
<div class="highlight highlight-source-powershell"><pre><span class="pl-c"><span class="pl-c">#</span> Example for Anthropic</span>
<span class="pl-smi">$<span class="pl-c1">env:</span>ANTHROPIC_API_KEY</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">"</span>your_key_here<span class="pl-pds">"</span></span>

<span class="pl-c"><span class="pl-c">#</span> Example for HuggingFace</span>
<span class="pl-smi">$<span class="pl-c1">env:</span>HUGGINGFACE_API_KEY</span> <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">"</span>another_key_here<span class="pl-pds">"</span></span></pre></div>
<div class="markdown-heading"><h4 class="heading-element"><strong>Step 2: Initialize Client</strong></h4><a id="user-content-step-2-initialize-client" class="anchor" aria-label="Permalink: Step 2: Initialize Client" href="#step-2-initialize-client"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<div class="highlight highlight-source-python"><pre><span class="pl-k">from</span> <span class="pl-s1">g4f</span>.<span class="pl-s1">client</span> <span class="pl-k">import</span> <span class="pl-v">Client</span>

<span class="pl-c"># Example for Anthropic</span>
<span class="pl-s1">client</span> <span class="pl-c1">=</span> <span class="pl-en">Client</span>(
    <span class="pl-s1">provider</span><span class="pl-c1">=</span><span class="pl-s">"g4f.Provider.Anthropic"</span>,
    <span class="pl-s1">api_key</span><span class="pl-c1">=</span><span class="pl-s">"your_key_here"</span>  <span class="pl-c"># Or use os.getenv("ANTHROPIC_API_KEY")</span>
)</pre></div>
<hr>
<div class="markdown-heading"><h3 class="heading-element"><strong>Synchronous Usage</strong></h3><a id="user-content-synchronous-usage" class="anchor" aria-label="Permalink: Synchronous Usage" href="#synchronous-usage"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<div class="highlight highlight-source-python"><pre><span class="pl-k">from</span> <span class="pl-s1">g4f</span>.<span class="pl-s1">client</span> <span class="pl-k">import</span> <span class="pl-v">Client</span>

<span class="pl-c"># Initialize with Anthropic</span>
<span class="pl-s1">client</span> <span class="pl-c1">=</span> <span class="pl-en">Client</span>(<span class="pl-s1">provider</span><span class="pl-c1">=</span><span class="pl-s">"g4f.Provider.Anthropic"</span>, <span class="pl-s1">api_key</span><span class="pl-c1">=</span><span class="pl-s">"your_key_here"</span>)

<span class="pl-c"># Simple request</span>
<span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-s1">client</span>.<span class="pl-c1">chat</span>.<span class="pl-c1">completions</span>.<span class="pl-c1">create</span>(
    <span class="pl-s1">model</span><span class="pl-c1">=</span><span class="pl-s">"claude-3.5-sonnet"</span>,
    <span class="pl-s1">messages</span><span class="pl-c1">=</span>[{<span class="pl-s">"role"</span>: <span class="pl-s">"user"</span>, <span class="pl-s">"content"</span>: <span class="pl-s">"Hello!"</span>}]
)
<span class="pl-en">print</span>(<span class="pl-s1">response</span>.<span class="pl-c1">choices</span>[<span class="pl-c1">0</span>].<span class="pl-c1">message</span>.<span class="pl-c1">content</span>)</pre></div>
<hr>
<div class="markdown-heading"><h3 class="heading-element"><strong>Asynchronous Usage</strong></h3><a id="user-content-asynchronous-usage" class="anchor" aria-label="Permalink: Asynchronous Usage" href="#asynchronous-usage"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<div class="highlight highlight-source-python"><pre><span class="pl-k">import</span> <span class="pl-s1">asyncio</span>
<span class="pl-k">from</span> <span class="pl-s1">g4f</span>.<span class="pl-s1">client</span> <span class="pl-k">import</span> <span class="pl-v">AsyncClient</span>

<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">main</span>():
    <span class="pl-c"># Initialize with Groq</span>
    <span class="pl-s1">client</span> <span class="pl-c1">=</span> <span class="pl-en">AsyncClient</span>(<span class="pl-s1">provider</span><span class="pl-c1">=</span><span class="pl-s">"g4f.Provider.Groq"</span>, <span class="pl-s1">api_key</span><span class="pl-c1">=</span><span class="pl-s">"your_key_here"</span>)
    
    <span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-s1">client</span>.<span class="pl-c1">chat</span>.<span class="pl-c1">completions</span>.<span class="pl-c1">create</span>(
        <span class="pl-s1">model</span><span class="pl-c1">=</span><span class="pl-s">"mixtral-8x7b"</span>,
        <span class="pl-s1">messages</span><span class="pl-c1">=</span>[{<span class="pl-s">"role"</span>: <span class="pl-s">"user"</span>, <span class="pl-s">"content"</span>: <span class="pl-s">"Hello!"</span>}]
    )
    <span class="pl-en">print</span>(<span class="pl-s1">response</span>.<span class="pl-c1">choices</span>[<span class="pl-c1">0</span>].<span class="pl-c1">message</span>.<span class="pl-c1">content</span>)

<span class="pl-s1">asyncio</span>.<span class="pl-c1">run</span>(<span class="pl-en">main</span>())</pre></div>
<hr>
<div class="markdown-heading"><h3 class="heading-element"><strong>Multiple Providers with API Keys</strong></h3><a id="user-content-multiple-providers-with-api-keys" class="anchor" aria-label="Permalink: Multiple Providers with API Keys" href="#multiple-providers-with-api-keys"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<div class="highlight highlight-source-python"><pre><span class="pl-k">import</span> <span class="pl-s1">os</span>
<span class="pl-k">import</span> <span class="pl-s1">g4f</span>.<span class="pl-v">Provider</span>
<span class="pl-k">from</span> <span class="pl-s1">g4f</span>.<span class="pl-s1">client</span> <span class="pl-k">import</span> <span class="pl-v">Client</span>

<span class="pl-c"># Using environment variables</span>
<span class="pl-s1">providers</span> <span class="pl-c1">=</span> {
    <span class="pl-s">"Anthropic"</span>: <span class="pl-s1">os</span>.<span class="pl-c1">getenv</span>(<span class="pl-s">"ANTHROPIC_API_KEY"</span>),
    <span class="pl-s">"Groq"</span>: <span class="pl-s1">os</span>.<span class="pl-c1">getenv</span>(<span class="pl-s">"GROQ_API_KEY"</span>)
}

<span class="pl-k">for</span> <span class="pl-s1">provider_name</span>, <span class="pl-s1">api_key</span> <span class="pl-c1">in</span> <span class="pl-s1">providers</span>.<span class="pl-c1">items</span>():
    <span class="pl-s1">client</span> <span class="pl-c1">=</span> <span class="pl-en">Client</span>(<span class="pl-s1">provider</span><span class="pl-c1">=</span><span class="pl-en">getattr</span>(<span class="pl-s1">g4f</span>.<span class="pl-c1">Provider</span>, <span class="pl-s1">provider_name</span>), <span class="pl-s1">api_key</span><span class="pl-c1">=</span><span class="pl-s1">api_key</span>)
    <span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-s1">client</span>.<span class="pl-c1">chat</span>.<span class="pl-c1">completions</span>.<span class="pl-c1">create</span>(
        <span class="pl-s1">model</span><span class="pl-c1">=</span><span class="pl-s">"claude-3.5-sonnet"</span>,
        <span class="pl-s1">messages</span><span class="pl-c1">=</span>[{<span class="pl-s">"role"</span>: <span class="pl-s">"user"</span>, <span class="pl-s">"content"</span>: <span class="pl-s">f"Hello to <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">provider_name</span><span class="pl-kos">}</span></span>!"</span>}]
    )
    <span class="pl-en">print</span>(<span class="pl-s">f"<span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">provider_name</span><span class="pl-kos">}</span></span>: <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">response</span>.<span class="pl-c1">choices</span>[<span class="pl-c1">0</span>].<span class="pl-c1">message</span>.<span class="pl-c1">content</span><span class="pl-kos">}</span></span>"</span>)</pre></div>
<hr>
<div class="markdown-heading"><h3 class="heading-element"><strong>Cookie-Based Authentication</strong></h3><a id="user-content-cookie-based-authentication" class="anchor" aria-label="Permalink: Cookie-Based Authentication" href="#cookie-based-authentication"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p><strong>For Providers Like Gemini/Bing</strong>:</p>
<ol>
<li>Open your browser and log in to the provider's website.</li>
<li>Use developer tools (F12) to copy cookies:
<ul>
<li>Chrome/Edge: <strong>Application</strong> → <strong>Cookies</strong>
</li>
<li>Firefox: <strong>Storage</strong> → <strong>Cookies</strong>
</li>
</ul>
</li>
</ol>
<div class="highlight highlight-source-python"><pre><span class="pl-k">from</span> <span class="pl-s1">g4f</span>.<span class="pl-s1">client</span> <span class="pl-k">import</span> <span class="pl-v">Client</span>
<span class="pl-k">from</span> <span class="pl-s1">g4f</span>.<span class="pl-v">Provider</span> <span class="pl-k">import</span> <span class="pl-v">Gemini</span>

<span class="pl-c"># Using with cookies</span>
<span class="pl-s1">client</span> <span class="pl-c1">=</span> <span class="pl-en">Client</span>(
    <span class="pl-s1">provider</span><span class="pl-c1">=</span><span class="pl-v">Gemini</span>,
)
<span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-s1">client</span>.<span class="pl-c1">chat</span>.<span class="pl-c1">completions</span>.<span class="pl-c1">create</span>(
    <span class="pl-s1">model</span><span class="pl-c1">=</span><span class="pl-s">""</span>, <span class="pl-c"># Default model</span>
    <span class="pl-s1">messages</span><span class="pl-c1">=</span><span class="pl-s">"Hello Google"</span>,
    <span class="pl-s1">cookies</span><span class="pl-c1">=</span>{
        <span class="pl-s">"__Secure-1PSID"</span>: <span class="pl-s">"your_cookie_value_here"</span>,
        <span class="pl-s">"__Secure-1PSIDTS"</span>: <span class="pl-s">"your_cookie_value_here"</span>
    }
)
<span class="pl-en">print</span>(<span class="pl-s">f"Gemini: <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">response</span>.<span class="pl-c1">choices</span>[<span class="pl-c1">0</span>].<span class="pl-c1">message</span>.<span class="pl-c1">content</span><span class="pl-kos">}</span></span>"</span>)</pre></div>
<hr>
<div class="markdown-heading"><h3 class="heading-element"><strong>Rate Limiting</strong></h3><a id="user-content-rate-limiting" class="anchor" aria-label="Permalink: Rate Limiting" href="#rate-limiting"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<div class="highlight highlight-source-python"><pre><span class="pl-k">from</span> <span class="pl-s1">aiolimiter</span> <span class="pl-k">import</span> <span class="pl-v">AsyncLimiter</span>

<span class="pl-c"># Limit to 5 requests per second</span>
<span class="pl-s1">rate_limiter</span> <span class="pl-c1">=</span> <span class="pl-en">AsyncLimiter</span>(<span class="pl-s1">max_rate</span><span class="pl-c1">=</span><span class="pl-c1">5</span>, <span class="pl-s1">time_period</span><span class="pl-c1">=</span><span class="pl-c1">1</span>)

<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">make_request</span>():
    <span class="pl-k">async</span> <span class="pl-k">with</span> <span class="pl-s1">rate_limiter</span>:
        <span class="pl-k">return</span> <span class="pl-k">await</span> <span class="pl-s1">client</span>.<span class="pl-c1">chat</span>.<span class="pl-c1">completions</span>.<span class="pl-c1">create</span>(...)</pre></div>
<hr>
<div class="markdown-heading"><h3 class="heading-element"><strong>Error Handling</strong></h3><a id="user-content-error-handling" class="anchor" aria-label="Permalink: Error Handling" href="#error-handling"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<div class="highlight highlight-source-python"><pre><span class="pl-k">from</span> <span class="pl-s1">tenacity</span> <span class="pl-k">import</span> <span class="pl-s1">retry</span>, <span class="pl-s1">stop_after_attempt</span>, <span class="pl-s1">wait_exponential</span>

<span class="pl-en">@<span class="pl-en">retry</span>(<span class="pl-s1">stop</span><span class="pl-c1">=</span><span class="pl-en">stop_after_attempt</span>(<span class="pl-c1">3</span>), <span class="pl-s1">wait</span><span class="pl-c1">=</span><span class="pl-en">wait_exponential</span>(<span class="pl-s1">multiplier</span><span class="pl-c1">=</span><span class="pl-c1">1</span>, <span class="pl-s1">min</span><span class="pl-c1">=</span><span class="pl-c1">4</span>, <span class="pl-s1">max</span><span class="pl-c1">=</span><span class="pl-c1">10</span>))</span>
<span class="pl-k">def</span> <span class="pl-en">safe_request</span>():
    <span class="pl-k">try</span>:
        <span class="pl-k">return</span> <span class="pl-s1">client</span>.<span class="pl-c1">chat</span>.<span class="pl-c1">completions</span>.<span class="pl-c1">create</span>(...)
    <span class="pl-k">except</span> <span class="pl-v">Exception</span> <span class="pl-k">as</span> <span class="pl-s1">e</span>:
        <span class="pl-en">print</span>(<span class="pl-s">f"Attempt failed: <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-en">str</span>(<span class="pl-s1">e</span>)<span class="pl-kos">}</span></span>"</span>)
        <span class="pl-k">raise</span></pre></div>
<hr>
<div class="markdown-heading"><h3 class="heading-element"><strong>Supported Providers</strong></h3><a id="user-content-supported-providers" class="anchor" aria-label="Permalink: Supported Providers" href="#supported-providers"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<table>
<thead>
<tr>
<th>Provider</th>
<th>Auth Type</th>
<th>Example Models</th>
</tr>
</thead>
<tbody>
<tr>
<td>Anthropic</td>
<td>API Key</td>
<td><code>claude-3.5-sonnet</code></td>
</tr>
<tr>
<td>Gemini</td>
<td>Cookies</td>
<td><code>gemini-1.5-pro</code></td>
</tr>
<tr>
<td>Groq</td>
<td>API Key</td>
<td><code>mixtral-8x7b</code></td>
</tr>
<tr>
<td>HuggingFace</td>
<td>API Key</td>
<td><code>llama-3.1-70b</code></td>
</tr>
</tbody>
</table>
<p><em>Full list: <a href="providers-and-models.html">Providers and Models</a></em></p>
<hr>
<div class="markdown-heading"><h2 class="heading-element"><strong>GUI Authentication</strong></h2><a id="user-content-gui-authentication" class="anchor" aria-label="Permalink: GUI Authentication" href="#gui-authentication"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<div class="markdown-heading"><h3 class="heading-element"><strong>Server Setup</strong></h3><a id="user-content-server-setup" class="anchor" aria-label="Permalink: Server Setup" href="#server-setup"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<ol>
<li>Create a password:
<div class="highlight highlight-source-shell"><pre><span class="pl-c"><span class="pl-c">#</span> Linux/macOS</span>
<span class="pl-k">export</span> G4F_API_KEY=<span class="pl-s"><span class="pl-pds">"</span>your_password_here<span class="pl-pds">"</span></span>

<span class="pl-c"><span class="pl-c">#</span> Windows (Command Prompt)</span>
<span class="pl-c1">set</span> G4F_API_KEY=your_password_here

<span class="pl-c"><span class="pl-c">#</span> Windows (PowerShell)</span>
<span class="pl-smi">$env</span>:G4F_API_KEY = <span class="pl-s"><span class="pl-pds">"</span>your_password_here<span class="pl-pds">"</span></span></pre></div>
</li>
<li>Start the server:
<div class="highlight highlight-source-shell"><pre>python -m g4f --debug --port 8080 --g4f-api-key <span class="pl-smi">$G4F_API_KEY</span></pre></div>
</li>
</ol>
<hr>
<div class="markdown-heading"><h3 class="heading-element"><strong>Browser Access</strong></h3><a id="user-content-browser-access" class="anchor" aria-label="Permalink: Browser Access" href="#browser-access"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<ol>
<li>Navigate to <code>http://localhost:8080/chat/</code>.</li>
<li>Use credentials:
<ul>
<li>
<strong>Username</strong>: Any value (e.g., <code>admin</code>).</li>
<li>
<strong>Password</strong>: Your <code>G4F_API_KEY</code>.</li>
</ul>
</li>
</ol>
<hr>
<div class="markdown-heading"><h3 class="heading-element"><strong>Programmatic Access</strong></h3><a id="user-content-programmatic-access" class="anchor" aria-label="Permalink: Programmatic Access" href="#programmatic-access"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<div class="highlight highlight-source-python"><pre><span class="pl-k">import</span> <span class="pl-s1">requests</span>

<span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-s1">requests</span>.<span class="pl-c1">get</span>(
    <span class="pl-s">"http://localhost:8080/chat/"</span>,
    <span class="pl-s1">auth</span><span class="pl-c1">=</span>(<span class="pl-s">"admin"</span>, <span class="pl-s">"your_password_here"</span>)
)
<span class="pl-en">print</span>(<span class="pl-s">"Success!"</span> <span class="pl-k">if</span> <span class="pl-s1">response</span>.<span class="pl-c1">status_code</span> <span class="pl-c1">==</span> <span class="pl-c1">200</span> <span class="pl-k">else</span> <span class="pl-s">f"Failed: <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">response</span>.<span class="pl-c1">status_code</span><span class="pl-kos">}</span></span>"</span>)</pre></div>
<hr>
<div class="markdown-heading"><h2 class="heading-element"><strong>Best Practices</strong></h2><a id="user-content-best-practices" class="anchor" aria-label="Permalink: Best Practices" href="#best-practices"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<ol>
<li>🔒 <strong>Never hardcode keys</strong>
<ul>
<li>Use <code>.env</code> files or secret managers like AWS Secrets Manager.</li>
</ul>
</li>
<li>🔄 <strong>Rotate keys every 90 days</strong>
<ul>
<li>Especially critical for production environments.</li>
</ul>
</li>
<li>📊 <strong>Monitor API usage</strong>
<ul>
<li>Use tools like Prometheus/Grafana for tracking.</li>
</ul>
</li>
<li>♻️ <strong>Retry transient errors</strong>
<ul>
<li>Use the <code>tenacity</code> library for robust retry logic.</li>
</ul>
</li>
</ol>
<hr>
<div class="markdown-heading"><h2 class="heading-element"><strong>Troubleshooting</strong></h2><a id="user-content-troubleshooting" class="anchor" aria-label="Permalink: Troubleshooting" href="#troubleshooting"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<table>
<thead>
<tr>
<th>Issue</th>
<th>Solution</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>"Invalid API Key"</strong></td>
<td>1. Verify key spelling<br>2. Regenerate key in provider dashboard</td>
</tr>
<tr>
<td><strong>"Cookie Expired"</strong></td>
<td>1. Re-login to provider website<br>2. Update cookie values</td>
</tr>
<tr>
<td><strong>"Rate Limit Exceeded"</strong></td>
<td>1. Implement rate limiting<br>2. Upgrade provider plan</td>
</tr>
<tr>
<td><strong>"Provider Not Found"</strong></td>
<td>1. Check provider name spelling<br>2. Verify provider compatibility</td>
</tr>
</tbody>
</table>
<hr>
<p><strong><a href="providers-and-models.html">Providers and Models →</a></strong></p>
<p><strong><a href="/docs/">← Return to Documentation</a></strong></p>
<p><strong><a href="#table-of-contents">⬆ Back to Top</a></strong></p>
</article>
</body>
</html>